<?php
/**
 * 离职管理
*/
apf_require_class("Bll_BLL");
apf_require_class('Bll_Hr_OutInterface');
apf_require_class('Const_Flow');
apf_require_class('Const_User');
apf_require_class('Bll_Flow_OperateLog');
class Bll_Flow_Dimission extends BLL {

    public function insert_leaving($data){
        $res = false;
        if(empty($data) || empty($data['user_id'])){
            return $res;
        }
        return $this->get_leaving_dao()->insert($data);
    }
    /**
     * 修改离职表单
     * @param int $leaving_id
     * @param array $data
     */
    public function update_leaving($leaving_id,$data){
        return $this->get_leaving_dao()->update_by_id($leaving_id,$data);
    }
    /**
     * 获取离职表单
     * @param int $leaving_id
     */
    public function find_by_id($leaving_id){
        return $this->get_leaving_dao()->find_by_id($leaving_id);
    }
    /**
     * 获取离职表单
     * @param int $flow_id
     */
    public function find_by_flow_id($flow_id){
        return $this->get_leaving_dao()->find_row(array('flow_id'=>$flow_id));
    }
    /**
     * 统计一个职位有几个人正在走流程，为招聘的在编人数使用
     */
    public function count_dimission_by_job($job_id){
        $_where = array(
                'now_status'=>1,
                'job_id'=>$job_id
        );
        return $this->get_leaving_dao()->find_count($_where);
    }
    /**
     * 员工离职
     * @param int $user_id
     * @param int $leaving_date 办理离职日期 20120202
     * @param int $last_date 最后工作日 20120202
     * @param int $leaving_type 离职类型
     * @param int $reason_id 离职原因
     * @param string $other_reason
     */
    public function sync_user_leaving($uid,$leaving_date, $last_date, $leaving_type, $reason_id, $other_reason = ''){
        if(empty($uid) || empty($leaving_date) || empty($last_date) || empty($leaving_type) || empty($reason_id)){
            return false;
        }
        $bll = new Bll_Hr_OutInterface();
        $params=array(
            'leaving_date'=>$leaving_date,
            'last_date'=>$last_date,
            'leaving_type'=>$leaving_type,
            'reason_id'=>$reason_id,
            'other_reason'=>$other_reason
        );
        return $bll->dimission_user($uid, $params);
    }
    /**
     * 写入离职申请|辞退员工录入
     * @param unknown_type $data
     * @return boolean
     */
    public function insert_retire_leaving($data,$is_dimiss=0){
        $res = false;
        if(empty($data) || empty($data['user_id'])){
            return $res;
        }
        $leaving_type = Const_User::USER_LEAVING_TYPE_ACTIVE;
        $flow_stage = Const_Flow::PHASE_29;
        if($is_dimiss){//辞退
            $leaving_type = Const_User::USER_LEAVING_TYPE_PASSIVE;
            $flow_stage = Const_Flow::PHASE_28;
        }
        $in_flow = array(
                'user_id'        => $data['user_id'],
                'user_name'      => $data['user_name'],
                'flow_type'      => Const_Flow::FLOW_TYPE_DIMISSION,
                'flow_stage'     => Const_Flow::FLOW_DIMISSION_1,//当前阶段
                'flow_opt'       => $flow_stage,
                'flow_role_list' => Const_Flow::ROLE_SUPERIOR,
                'work_city_id'   => $data['work_city_id'],
                'effect_date'    => $data['plan_date'],
                'now_status'     => 1,
        );
        apf_require_class('Bll_Flow_Info');
        $flow = new Bll_Flow_Info();
        $flow_id = $flow->insert_flow($in_flow);

        if($flow_id){
            $in = array(
                    'flow_id'      => $flow_id,
                    'user_id'      => $data['user_id'],
                    'opt_user_id'  => $data['opt_user_id'],
                    'job_id'       => $data['job_id'],
                    'leaving_type' => $leaving_type,
                    'reason_id'    => $data['reason_id'],
                    'hr_leaving_type' => $leaving_type,
                    'hr_reason_id'    => $data['reason_id'],
                    'other_reason' => trim($data['other_reason']),
                    'plan_date'    => $data['plan_date'],
                    'last_date'    => $data['plan_date'],
                    'leaving_date' => $data['plan_date'],
                    'created'      => time(),
                    'now_status'   => 1
            );

            $l_id = $this->get_leaving_dao()->insert($in);
            //save log
            $log_data = array(
                    'user_id'      => $data['user_id'],
                    'user_name'    => $data['user_name'],
                    'opt_user_id'  => $data['opt_user_id'],
                    'role_type'    => $data['role_id'],
                    'work_city_id' => $data['work_city_id'],
                    'flow_id'      => $flow_id,
                    'flow_opt'     => $flow_stage,
                    'flow_stage'   => Const_Flow::FLOW_DIMISSION_1,
                    'flow_type'    => Const_Flow::FLOW_TYPE_DIMISSION,
                    'effect_date'  => $data['plan_date'],
            );
            $log_bll = new Bll_Flow_OperateLog();
            $log_bll->save_log($log_data);
        }
        return $flow_id;
    }
    /**
     *
     * @param unknown_type $flow_ids
     * @return multitype:
     */
    public function find_by_flow_ids($flow_ids,$is_short=1){
        $data = array();
        if(empty($flow_ids)){
            return $data;
        }
        $_where["flow_id IN(".implode(',',$flow_ids).") AND 1=?"]=1;
        $field = array();
        if($is_short){
            $field = array('flow_id','user_id','job_id');
        }
        $tmp_data = $this->get_leaving_dao()->find_short($_where, "", 0,0,$field);
        if(!empty($tmp_data)){
            foreach($tmp_data as $val){
                $data[$val['flow_id']] = $val;
            }
        }
        return $data;
    }

    /**
     * 如果员工离职需要清理和用户相关的flow相关事项
     * @param unknown_type $uid
     * @return boolean
     */
    public function clear_user_flow($uid){
        //转正,续签
        apf_require_class('Bll_Flow_UserRegular');
        apf_require_class('Bll_Flow_UserRenew');
        //清理转正
        $reg_bll = new Bll_Flow_UserRegular();
        $reg_bll->update_by_dimission($uid);
        //清理续签
        $renew_bll = new Bll_Flow_UserRenew();
        $renew_bll->update_by_dimission($uid);
        //员工离职后清除对应角色关系
        apf_require_class('Bll_Flow_Role');
        $role_bll = new Bll_Flow_Role();
        $role_bll->delete_role_by_uid($uid);
        return true;
    }

    public function get_leaving_dao(){
        $dao = $this->get_dao('Dao_Flow_Leaving');
        return $dao;
    }
}